﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Displays an Image uploaded to the cms database and renders approreate class height and width depending on which CMS Option of Left Right and Center for the FLoat setting
    /// </summary>
    public class FloatingImageContentControl : BaseContentControlDisplay<FloatingImageContentControlProperties>
    {
        private AcmsImage image = new AcmsImage();

        protected override void CreateChildControls()
        {
            this.Controls.Add(image);
            base.CreateChildControls();
        }

        public override void PropertiesLoaded(FloatingImageContentControlProperties properties)
        {
            base.PropertiesLoaded(properties);
            if (properties != null)
            {
                this.Float = properties.Float;
            }
        }

        protected override void DataBindChildren()
        {
            if (Properties != null)
            {
                image.FileId = Properties.ResourceId;
                if (Properties.Float == FloatingImageContentControlProperties.FloatOptions.Left)
                {
                    image.RenderHeight = this.LeftRenderHeight;
                    image.RenderWidth = this.LeftRenderWidth;
                }
                else if (Properties.Float == FloatingImageContentControlProperties.FloatOptions.Right)
                {
                    image.RenderHeight = this.RightRenderHeight;
                    image.RenderWidth = this.RightRenderWidth;
                }
                else if (Properties.Float == FloatingImageContentControlProperties.FloatOptions.Centre)
                {
                    image.RenderHeight = this.CenterRenderHeight;
                    image.RenderWidth = this.CenterRenderWidth;
                }

                image.AlternateText = Properties.AltText;
            }
        }

        private FloatingImageContentControlProperties.FloatOptions Float { get; set; }

        protected override object SaveViewState()
        {
            ViewState["Float"] = (int)Float;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if(ViewState["Float"] is int)
                Float = (FloatingImageContentControlProperties.FloatOptions)(int)ViewState["Float"];
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Visible)
            {
                StringBuilder cssClass = new StringBuilder();
                cssClass.Append(this.CssClass);
                cssClass.Append(" ");
                switch (Float)
                {
                    case FloatingImageContentControlProperties.FloatOptions.Left:
                        cssClass.Append(LeftCssClass);
                        break;
                    case FloatingImageContentControlProperties.FloatOptions.Right:
                        cssClass.Append(RightCssClass);
                        break;
                    case FloatingImageContentControlProperties.FloatOptions.Centre:
                        cssClass.Append(CenterCssClass);
                        break;
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass.ToString().Trim());
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                image.RenderControl(writer);
                writer.RenderEndTag();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.image == null || this.image.FileId == null || this.image.FileId == 0)
            {
                this.Visible = false;
            }
        }

        public override bool IsEmpty
        {
            get
            {
                return Properties == null || !Properties.HasResource();
            }
        }

        /// <summary>
        /// Class to render if the image is Left Floated
        /// </summary>
        public string LeftCssClass{ get; set; }
        /// <summary>
        /// Class to render if the image is Right Floated
        /// </summary>
        public string RightCssClass { get; set; }
        /// <summary>
        /// Class to render if the image is Centered
        /// </summary>
        public string CenterCssClass { get; set; }

        /// <summary>
        /// Width to render if the image is Left Floated
        /// </summary>
        public int LeftRenderWidth { get; set; }
        /// <summary>
        /// Height to render if the image is Left Floated
        /// </summary>
        public int LeftRenderHeight { get; set; }
        /// <summary>
        /// Width to render if the image is Right Floated
        /// </summary>
        public int RightRenderWidth { get; set; }
        /// <summary>
        /// Height to render if the image is Right Floated
        /// </summary>
        public int RightRenderHeight { get; set; }
        /// <summary>
        /// Width to render if the image is Centered
        /// </summary>
        public int CenterRenderWidth { get; set; }
        /// <summary>
        /// Heieht to render if the image is Centered
        /// </summary>
        public int CenterRenderHeight { get; set; }

    }
}
